//
//  MonitorViewController.m
//  ICrEAM
//
//  Created by kuroro on 16/11/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MonitorViewController.h"
#import "MonitorInfoFirstView.h"
#import "MonitorInfoSecondView.h"
#import "MonitorInfoThirdView.h"
#define kHeight 140
#define kWidth 120
@interface MonitorViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *bigScrollView;
@property (nonatomic,strong) UIView *firstView;
@property (nonatomic,strong) UIView *secondView;
@property (nonatomic,strong) UIView *thirdView;
@property (nonatomic,strong) NSDictionary *firstData;
@property (nonatomic,strong) NSDictionary *secondData;
@property (nonatomic,strong) NSDictionary *thirdData;
@property(nonatomic,strong)UIView *coverView;
@property(nonatomic,strong)UIView *coverView2;
@property(nonatomic,strong)UIView *coverView3;
@property(nonatomic,strong)UIView *coverView1;

@end

@implementation MonitorViewController

-(UIView *)coverView{
    if (!_coverView) {
        _coverView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth , kHeight/2.)];
        _coverView.userInteractionEnabled=YES;
        _coverView.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close:)];
        [_coverView addGestureRecognizer:tap];
    }
    return _coverView;
}
-(UIView *)coverView1{
    if (!_coverView1) {
    
        _coverView1=[[UIView alloc]initWithFrame:CGRectMake(0, kHeight/2.,kWidth/2. , ScreenHeight-kHeight)];
        _coverView1.userInteractionEnabled=YES;
        _coverView1.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close:)];
        [_coverView1 addGestureRecognizer:tap];
    }
    return _coverView1;
}
-(UIView *)coverView2{
    if (!_coverView2) {
        _coverView2=[[UIView alloc]initWithFrame:CGRectMake(0,  ScreenHeight-kHeight/2.,ScreenWidth , kHeight/2.)];
        _coverView2.userInteractionEnabled=YES;
        _coverView2.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close:)];
        [_coverView2 addGestureRecognizer:tap];
    }
    return _coverView2;
}
-(UIView *)coverView3{
    if (!_coverView3) {
        _coverView3=[[UIView alloc]initWithFrame:CGRectMake(ScreenWidth-kWidth/2, kHeight/2.,kWidth/2 ,ScreenHeight-kHeight)];
        _coverView3.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(close:)];
        [_coverView3 addGestureRecognizer:tap];
    }
    return _coverView3;
}

-(void)close:(UITapGestureRecognizer*)rec{
    CGPoint point=[rec locationInView:self.coverView];
    CGRect rect=[self.view convertRect:self.view .bounds toView:[UIApplication sharedApplication].keyWindow];
    if (CGRectContainsPoint(rect, point)) {
        
        
    }else{
        [self dismissViewControllerAnimated:YES completion:^{
            [self.coverView removeFromSuperview];
            [self.coverView1 removeFromSuperview];
            [self.coverView2 removeFromSuperview];
            [self.coverView3 removeFromSuperview];
        }];
    }
}

-(void)loadView{
    [super loadView];
    self.hideRightNavItem = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication].keyWindow addSubview:self.coverView];
     [[UIApplication sharedApplication].keyWindow addSubview:self.coverView1];
     [[UIApplication sharedApplication].keyWindow addSubview:self.coverView2];
     [[UIApplication sharedApplication].keyWindow addSubview:self.coverView3];
 
    self.preferredContentSize = CGSizeMake(ScreenWidth-ActualWidth(120), ScreenHeight-140);

    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.view.backgroundColor = BackgroundColor_Gray;
    [self firstMonitorInfo];
    [self setUpButton];
    self.index = self.index?:0;
    
}

- (void)setupBigScrollView{
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-ActualWidth(120), ScreenHeight-kHeight)];
    self.bigScrollView.contentSize = CGSizeMake((ScreenWidth-ActualWidth(120))*3, ScreenHeight-kHeight);
    //
    self.firstView = [MonitorInfoFirstView creatMonitorInfoFirstViewWithDic:self.firstData];
    self.firstView.frame = CGRectMake(0, 0, ScreenWidth-ActualWidth(120), ScreenHeight-kHeight);
    self.secondView = [MonitorInfoSecondView creatMonitorInfoSecondViewWithDic:self.secondData];
    self.secondView.frame = CGRectMake(ScreenWidth-ActualWidth(120), 0, ScreenWidth-ActualWidth(120), ScreenHeight-kHeight);
    self.thirdView = [MonitorInfoThirdView creatMonitorInfoThirdViewWithDic:self.thirdData];
    self.thirdView.frame = CGRectMake(2*(ScreenWidth-ActualWidth(120)), 0, ScreenWidth-ActualWidth(120), ScreenHeight-kHeight);
    [self.bigScrollView addSubview:self.firstView];
    [self.bigScrollView addSubview:self.secondView];
    [self.bigScrollView addSubview:self.thirdView];
       //
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.delegate = self;
//    self.bigScrollView.
    [self.bigScrollView scrollRectToVisible:CGRectMake((ScreenWidth-ActualWidth(120))*self.index, 0, ScreenWidth-ActualWidth(120), ScreenHeight-kHeight) animated:YES];
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.bigScrollView];
}

- (void)setUpButton{
    __weak typeof(self) weakSelf = self;
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setTitle:@"<" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [leftButton addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view.mas_left);
        make.top.equalTo(weakSelf.view.mas_top);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
        make.width.equalTo(@(ActualWidth(70)));
    }];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@">" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [rightButton addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.view.mas_right);
        make.top.equalTo(weakSelf.view.mas_top);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
        make.width.equalTo(@(ActualWidth(70)));
    }];


}

-(void)clickLeft{
    if (self.index<=0) {
        return;
    }
    self.index--;
    [self.bigScrollView scrollRectToVisible:CGRectMake((ScreenWidth-ActualWidth(140))*self.index, 0, ScreenWidth-ActualWidth(140), ScreenHeight-NavBarAndStatusHeight) animated:YES];
}

-(void)clickRight{
    if (self.index>=2) {
        return;
    }
    self.index++;
    [self.bigScrollView scrollRectToVisible:CGRectMake((ScreenWidth-ActualWidth(140))*self.index, 0, ScreenWidth-ActualWidth(140), ScreenHeight-NavBarAndStatusHeight) animated:YES];
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //float offsetX = scrollView.contentOffset.x;
     self.index = roundf(scrollView.contentOffset.x/(ScreenWidth-ActualWidth(140)));
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//    float offsetX = scrollView.contentOffset.x;
     self.index = roundf(scrollView.contentOffset.x/(ScreenWidth-ActualWidth(140)));
}

- (void)firstMonitorInfo{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    [parameters setObject:self.vin forKey:@"VIN"];
    [manager startGetRequestWithURL:@"/INFO_ICREAM_WEB/App/queryVehicleMonitorFirstPart" parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            self.firstData = data;
            [self secondMonitorInfo];
        }
    }];
}

- (void)secondMonitorInfo{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    if (self.RO_NO) {
        [parameters setObject:self.RO_NO forKey:@"RO_NO"];
    }
    [parameters setObject:self.vin forKey:@"VIN"];
    [manager startGetRequestWithURL:@"/INFO_ICREAM_WEB/App/queryVehicleMonitorSecondPart" parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            self.secondData = data;
            [self thirdMonitorInfo];
        }
    }];
    
}

- (void)thirdMonitorInfo{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    [parameters setObject:self.vin forKey:@"VIN"];
    [manager startGetRequestWithURL:@"/INFO_ICREAM_WEB/App/queryVehicleMonitorThirdPart" parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            self.thirdData = data;
            [self setupBigScrollView];
        }
    }];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.hideRightNavItem = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
